﻿using System.Collections.Generic;
using System.Runtime.CompilerServices;

public static class UnsafeEx
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void SkipInit<T>(out T value)
    {
        value = default;
    }

    public static bool TryPop<T>(this Stack<T> self, out T result)
    {
        if (self.Count > 0)
        {
            result = self.Pop();
            return true;
        }
        result = default;
        return false;
    }

    public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> self, TKey key, TValue value)
    {
        if (!self.ContainsKey(key))
        {
            self.Add(key, value);
            return true;
        }
        return false;
    }

    public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
    {
        return dictionary.TryGetValue(key, out TValue value) ? value : defaultValue;
    }
}